if (top.window && top.window.LOADING) {
    window.LOADING = top.window.LOADING;
} else {
    var loadingModal = $('<div id="LODAING" class="modal hide fade" tabindex="-1" data-backdrop="static" style="width: 60px;height: 62px;left: 50%;margin-left:-25px;top: 50%;margin-top: -25px; " role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-body" style="padding: 5px;"><img src="/ubang/res/images/loading_circle.gif"/></div></div>');
    window.LOADING = {
        modal: null,
        show: function () {
//            if ($(".modal-backdrop").length > 0) {
//                $(".modal-backdrop").remove();
//            }
            if (!this.modal) {
                this.modal = loadingModal;
            }
            if (!this.isShow) {
                this.modal.modal("show");
                this.isShow = true;
            }

        },
        hide: function () {
            if (!this.modal) {
                this.modal = loadingModal;
            }
            this.modal.modal("hide");
            this.isShow = false;
        },
        isShow: false
    }
}

$.ajaxSetup({
    success: function (data) {
        LOADING.hide();
    }, error: function (xhr, status, e) {
        LOADING.hide();
    },//完成请求后触发。即在success或error触发后触发
    complete: function (xhr, status) {
        LOADING.hide();
    },
    beforeSend: function (xhr) {
        LOADING.show();
//        setTimeout(function () {
//            LOADING.hide();
//        }, 2000);
    }
});

angular.module('ubang', []);
(function () {
    /******************************************/
    var _config = {}
    _config.user_token_name = '___USER_TOKEN';
    _config.device_id_name = 'deviceId';

    _config.temp_v = 0.001;
    _config.system_version = '';
    _config.api_version = _config.system_version;
    _config.base_url = _config.api_version + '/services/api';

    _config.url_user = _config.base_url + '/account/user';
    _config.url_role = _config.base_url + '/account/role';
    _config.url_group = _config.base_url + '/account/group';
    _config.url_user_group = _config.base_url + '/account/user_group';
    _config.url_resource = _config.base_url + '/account/resource';
    _config.url_resource_group = _config.base_url + '/account/resource_group';
    _config.url_device = _config.base_url + '/common/device';
    _config.url_update_log = _config.base_url + '/common/update_log';
    _config.url_device_flow = _config.base_url + '/common/device_flow';
    window.config = _config;
    /******************************************/

    var _initAngular = function ($module) {
        angular.module($module, []);
    }

    var _applyAngularScope = function ($rootScope, $scope) {
        if (!$rootScope.$$phase) {
            $scope.$apply();
        }
    }

    window.applyAngularScope = _applyAngularScope;
    window.initAngular = _initAngular;

})();
//$(document).ready(function () {
//    var loadingDiv = "<div id='loading-mask'></div><div id='loading'><div><img src='/ubang/res/images/loading_circle.gif' width='20px' height='20spx'/>&nbsp;<span id='loading-msg'>初始化数据...</span></div></div>";
//    $("body").prepend(loadingDiv);
//    $("#loading-mask").css({ "position": "absolute", "left": "0px", "top": "0px", "width": "100%", "height": "100%", "z-index": "20000", "background-color": "#FFFFFF", "filter": "alpha(opacity=100)", "-moz-opacity": "1", "-khtml-opacity": "1", "opacity": "1"});
//    $("#loading").css({ "position": "absolute", "left": "45%", "top": "40%", "padding": "2px", "width": "150px", "text-align": "center", "background-color": "#FFFFFF", "z-index": "20001", "height": "auto"});
//    $("#loading-msg").css({ "font-size": "12px" });
//    loading('数据加载中...', 50);
//    loading('数据加载完成', 90);
//    loading('数据加载完成', 100);
//});
//
//var loading = function (a, b) {
//    var c = b * 1.5;
//    if (b == 100) {
//        $('#loading').animate({width: '150px'}, 200, function () {
//            $('#loading-msg').html(a);
//            $("#loading").css("display", "none");
//            $("#loading-mask").css("display", "none");
//        });
//    } else {
//        $('#loading').animate({width: '150px'}, 200, function () {
//            $('#loading-msg').html(a);
//        });
//    }
//};

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

//移除数组中的元素
Array.prototype.remove = function (dx) {
    if (isNaN(dx) || dx > this.length) {
        return false;
    }
    delete this[dx];
    for (var i = 0, n = 0; i < this.length; i++) {
        if (this[i] != this[dx]) {
            this[n++] = this[i];
        }
    }
    this.length -= 1;
}